#include "dominion.h"
#include <limits.h>


#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include <CUnit/Automated.h>
#include <CUnit/Console.h>
#include <CUnit/TestDB.h>
#include <stdio.h>


// #include "sum.h"


static FILE *temp_file = NULL;

int init_suite1(void)
{
    if (NULL == (temp_file = fopen("temp.txt", "w+")))
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

int clean_suite1(void)
{
    if (0 != fclose(temp_file))
    {
        return -1;
    }
    else
    {
        temp_file = NULL;
        return 0;
    }
}

// compare from doinion.c
int compare(const void* a, const void* b) {
  if (*(int*)a > *(int*)b)
    return 1;
  if (*(int*)a < *(int*)b)
    return -1;
  return 0;
}


void test_compare(void)
{
    // arguments to compare are integers
    for (int a = -3; a < 4; a++)
    {
        for (int b = -3; b < 4; b++)
        {
            CU_ASSERT(compare(&a, &b) == ((a - b) > 0) - ((a - b) < 0) );
        }
    }
    // test cases where arguments are MIN_INT or MAX_INT for over/underflow *************************************
    int a = INT_MIN; 
    int b = 1;
    CU_ASSERT(compare(&a, &b) == -1 );
    CU_ASSERT(compare(&a, &a) == 0 );
    CU_ASSERT(compare(&b, &a) == 1 );
    a = INT_MAX; b = 1;
    CU_ASSERT(compare(&a, &b) == 1 );
    CU_ASSERT(compare(&a, &a) == 0 );
    CU_ASSERT(compare(&b, &a) == -1 );

}









int main(int argc, char **argv)
{

    CU_pSuite pSuite = NULL;


    if (CU_initialize_registry() == CUE_SUCCESS)
    {
        printf("OK\n");
    }

    pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
    if (pSuite == NULL)
    {
        CU_cleanup_registry();
        return CU_get_error();
    }

    CU_add_test(pSuite, "Test of compare()", test_compare);

    CU_basic_set_mode(CU_BRM_VERBOSE);
    CU_basic_run_tests();
    CU_cleanup_registry();
    return CU_get_error();


}





